<template>
  <a-layout class="homeLayout">
    <!-- 头部 -->
    <a-layout-header class="head">
      <Head></Head>
    </a-layout-header>

    <a-layout style="height: 90%">
      <a-layout-sider breakpoint="lg" collapsed-width="60" theme="light">
        <Sider :number="2" />
      </a-layout-sider>

      <a-layout>
        <a-layout-content class="content">
          <router-view />
        </a-layout-content>
      </a-layout>
    </a-layout>
  </a-layout>
</template>

<script lang="ts" setup>
import Head from '@/layout/Head.vue';
import Sider from '@/layout/Sider.vue';
import { computed, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
import CourseDao from '@/daos/course_dao';
import { message } from 'ant-design-vue';

const route = useRoute();
const store = useStore();
const courseDao = new CourseDao();

const key = computed(() => {
  return route.path;
});

// 从路由参数获取课程ID
const courseId = computed(() => {
  const id = route.params.courseId ? Number(route.params.courseId) : null;
  console.log(`课程详情页 - 路由参数中的课程ID: ${id}`);
  return id;
});

// 获取课程信息并设置到store
const loadCourseInfo = async () => {
  if (!courseId.value) {
    console.error('课程ID不存在，无法加载课程信息');
    return;
  }
  
  try {
    console.log(`开始加载课程信息，ID: ${courseId.value}`);
    
    // 先设置课程ID到store
    store.commit('course/SET_COURSEID', courseId.value);
    
    // 获取课程详细信息
    const result = await courseDao.findCourse(courseId.value, null);
    console.log('课程信息加载结果:', result);
    
    if (result?.data?.data?.[0]) {
      const courseInfo = result.data.data[0];
      // 设置项目列表和教师列表
      store.commit('course/SET_PROJECTS', courseInfo.projectList || []);
      store.commit('course/SET_TEACHERS', courseInfo.teacherList || []);
      console.log('课程信息已加载到store中');
    } else {
      console.warn('未找到课程信息:', result);
      message.warning('未能获取完整的课程信息');
    }
  } catch (error) {
    console.error('加载课程信息失败:', error);
    message.error('加载课程信息失败，请返回课程列表重新进入');
  }
};

onMounted(() => {
  console.log('课程详情页挂载，路由参数:', route.params);
  loadCourseInfo();
});
</script>
<style scoped>
.homeLayout {
  background-color: rgb(242, 247, 250);
  height: 100vh;
}

.head {
  background: #fff;
  width: 100%;
  height: auto;
  border-bottom: 1px solid #eee;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.031372549);
}

.content {
  height: 100%;
  background: white;
  overflow: auto;
}

/* 切换动画 */
.fade-transform-leave-active,
.fade-transform-enter-active {
  transition: all 0.3s;
}

.fade-transform-enter {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

::-webkit-scrollbar {
  width: 5px;
  /* 滚动条的宽度 */
  height: 5px;
  /* 对于水平滚动条 */
}

::-webkit-scrollbar-thumb {
  background: #888;
  /* 滚动条滑块的颜色 */
  border-radius: 10px;
  /* 滑块边角圆滑度 */
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
  /* 滚动条轨道的颜色 */
  border-radius: 10px;
  /* 轨道边角圆滑度 */
}
</style>
